package org.tmsps.ne4spring.utils.img;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;

public class ImgReadToBase64Tools {

	/**
	 * 图片生成base64编码
	 * 
	 * @param imgPath
	 * @return
	 */
	public static String readImgToBase64(String imgPath) {
		String result = "";
		File imgFile = new File(imgPath);
		if (!imgFile.exists()) {
			System.out.println("非法路径");
			return result;
		}
		InputStream in = null;
		byte[] data = null;
		// 读取图片字节数组
		try {
			in = new FileInputStream(imgFile);
			data = new byte[in.available()];
			in.read(data);
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		// 对字节数组Base64编码
		//return "data:image/png;base64," + Base64.getEncoder().encodeToString(data);
		return Base64.getEncoder().encodeToString(data);
	}

}
